package com.shushan.mixedexam.englishwords;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.base.BaseFragment;
import com.shushan.mixedexam.adapter.EnglishWordsStudyRecyclerAdapter;
import com.shushan.pages.newtype.bean.EnglishWordsEntryItemBean;
import com.shushan.pages.newtype.bean.ExampleSentenceBean;
import com.shushan.pages.newtype.bean.InterpretationBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: EnglishWordsStudyFragment
 * @Desciption: //英语单词学习fragment
 * @author: zhangshihao
 * @date: 2018-06-28
 */
public class EnglishWordsStudyFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private TextView tvWord;
    private TextView tvSoundMark;
    private ImageView ivTrumpet;
    private TextView tvWordTranslation;

    private AnimationDrawable playAnim;
    private int examIndex = 0;
    private EnglishWordsEntryItemBean entryItemBean;

    private OnTrumpetClickListener onTrumpetClickListener;

    public static EnglishWordsStudyFragment newInstance(int examIndex,EnglishWordsEntryItemBean entryItemBean){
        EnglishWordsStudyFragment studyFragment = new EnglishWordsStudyFragment();
        studyFragment.setEntryItemBean(entryItemBean);
        studyFragment.setExamIndex(examIndex);

        return studyFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_english_words_study,null,false);

        initView(view);

        initData();

        return view;
    }

    private void initView(View contentView){
        recyclerView = contentView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), RecyclerView.VERTICAL));
        tvWord = contentView.findViewById(R.id.tv_word);
        tvSoundMark = contentView.findViewById(R.id.tv_soundmark);
        ivTrumpet = contentView.findViewById(R.id.iv_trumpet);
        ivTrumpet.setImageResource(R.drawable.anim_audio_play);
        playAnim = (AnimationDrawable) ivTrumpet.getDrawable();
        tvWordTranslation = contentView.findViewById(R.id.tv_word_translation);
    }

    private void initData(){
        if(entryItemBean == null){
            return;
        }
        ivTrumpet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onTrumpetClickListener != null){
                    onTrumpetClickListener.onTrumpetClick(entryItemBean.id,playAnim);
                }
            }
        });
        tvWord.setText(entryItemBean.new_word);
        tvSoundMark.setText(entryItemBean.pronunciation);
        List<InterpretationBean> interpretationBeans = entryItemBean.getInterpretationBeans();
        if(!ToolUtils.isListEmpty(interpretationBeans)){
            StringBuffer sb = new StringBuffer();
            for(int i=0;i<interpretationBeans.size();i++){
                InterpretationBean bean = interpretationBeans.get(i);
                sb.append(bean.type)
                  .append(bean.name);
                if(i != (interpretationBeans.size()-1)){//非最后一个
                    sb.append(";  ");
                }
            }
            tvWordTranslation.setText(sb.toString());
        }
        List<ExampleSentenceBean> sentenceBeans = entryItemBean.getExampleSentenceBeans();
        if(!ToolUtils.isListEmpty(sentenceBeans)) {
            EnglishWordsStudyRecyclerAdapter recyclerAdapter = new EnglishWordsStudyRecyclerAdapter(entryItemBean.new_word,sentenceBeans);
            recyclerView.setAdapter(recyclerAdapter);
        }

    }

    public EnglishWordsEntryItemBean getEntryItemBean() {
        return entryItemBean;
    }

    public void setEntryItemBean(EnglishWordsEntryItemBean entryItemBean) {
        this.entryItemBean = entryItemBean;
    }

    public int getExamIndex() {
        return examIndex;
    }

    public void setExamIndex(int examIndex) {
        this.examIndex = examIndex;
    }

    public AnimationDrawable getPlayAnim() {
        return playAnim;
    }

    public void setPlayAnim(AnimationDrawable playAnim) {
        this.playAnim = playAnim;
    }

    public void setOnTrumpetClickListener(OnTrumpetClickListener onTrumpetClickListener) {
        this.onTrumpetClickListener = onTrumpetClickListener;
    }

}
